<?php

namespace app\api\controller\v1;
use app\api\validate\Count;
use app\api\validate\IDMustBePositiveInt;
use app\api\model\Product as ProductModel;
use app\lib\exception\ProductException;
use app\lib\exception\ProductCategoryException;

class Product{
    public function getRecent($count=15)
    {
        (new Count())->goCheck();
        $product = ProductModel::getMostRecent($count);
        if($product->isEmpty()){
            throw new ProductException();
        }
        //隐藏字段
        $product = $product->hidden(['summary']);
        return $product;
    }
    public function getAllInCategory($id)
    {
        (new IDMustBePositiveInt())->goCheck();
        
        $category = ProductModel::getByCategoryID($id);
        if($category->isEmpty()){
            throw new ProductCategoryException();
        }
        return $category;
    }
    public function getOne($id)
    {
        (new IDMustBePositiveInt())->goCheck();
        $product = ProductModel::getProductDetail($id);

        if(!$product){
            throw new ProductException();
        }
        return $product;
    }
}